﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace nftsHardLink
{
    public partial class Form1 : Form
    {
        readonly FolderBrowserDialog _fbd;


        public Form1()
        {
            InitializeComponent();
            _fbd = new FolderBrowserDialog
            {
                Description = @"请选择目标文件夹目录",
                ShowNewFolderButton = true,
                RootFolder = Environment.SpecialFolder.Desktop
            };

        }

        private void tbTarget_DoubleClick(object sender, EventArgs e)
        {

            var result = _fbd.ShowDialog();

            if (result == DialogResult.OK)
            {
                string folderName = _fbd.SelectedPath;
                if (folderName != "")
                {
                    tbTarget.Text = folderName;
                }
            }
        }

        private void tbSource_DoubleClick(object sender, EventArgs e)
        {
            DialogResult result = _fbd.ShowDialog();

            if (result == DialogResult.OK)
            {
                string folderName = _fbd.SelectedPath;
                if (folderName == string.Empty)
                {
                    return;
                } tbSource.Text = folderName;
            }
            string tbSourcefolderName = _fbd.SelectedPath;
            int sourecefolderindex = tbSourcefolderName.LastIndexOf("\\", StringComparison.Ordinal);
            int targetfolderindex = tbTarget.Text.LastIndexOf("\\", StringComparison.Ordinal);
            string sourcefoldername = tbSourcefolderName.Substring(sourecefolderindex);
            string targetfoldername = tbTarget.Text.Substring(targetfolderindex);
            tbHistory.AppendText("target:" + targetfoldername + " source:" + sourcefoldername + Environment.NewLine);
            if (sourcefoldername != targetfoldername)
            {
                tbTarget.AppendText(sourcefoldername);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var targetFolder = tbTarget.Text.Trim();
            var sourceFolder = tbSource.Text.Trim();

            var strInfo = CreateNtfsHardLink(targetFolder, sourceFolder);
            tbHistory.AppendText(strInfo + Environment.NewLine);
        }

        private static string CreateNtfsHardLink(string targetFolder, string sourceFolder)
        {
            var p = new Process
            {
                StartInfo =
                {
                    FileName = "cmd.exe",
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                }
            };
            p.Start();

            var cmd = string.Format("mklink /j \"{0}\" \"{1}\"",
                targetFolder, sourceFolder);
            p.StandardInput.WriteLine(cmd + "&exit");

            //从输出流获取命令执行结果， 
            //string exepath = Application.StartupPath;
            //把返回的DOS信息读出来 
            return p.StandardOutput.ReadToEnd();
        }
    }
}
